﻿<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>资源编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../assets/layui/css/layui.css" media="all">
    <!-- fontIconPicker core CSS -->
    <link rel="stylesheet" type="text/css" href="../assets/lib/fonticonpicker/css/base/jquery.fonticonpicker.min.css" />
    <link rel="stylesheet" type="text/css" href="../assets/lib/fonticonpicker/css/themes/grey-theme/jquery.fonticonpicker.grey.min.css" />
    <link rel="stylesheet" type="text/css" href="../assets/font-awesome-4.7.0/css/font-awesome.css" />
</head>

<body>
<fieldset class="layui-elem-field layui-field-title" style="padding-top: 5px;border: hidden">
    <form class="layui-form" action="" lay-filter="dataForm">
        <input type="hidden" name="id" id="ipt_id">
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <label class="layui-form-label"><span style="color: red;">*</span>资源名称</label>
            <div class="layui-input-block">
                <input type="text" id="ipt_name" name="name" lay-verify="required" required placeholder="名称"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <label class="layui-form-label">资源图标</label>
            <div class="layui-input-block">
                <input type="text" id="ipt_icon" lay-filter="iconPicker"  name="icon" lay-ignore>
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11" pane="">
            <label class="layui-form-label"><span style="color: red;">*</span>上级节点</label>
            <div class="layui-input-block">
                <select id="ddl_parentId" name="parentId">
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <label class="layui-form-label">URL</label>
            <div class="layui-input-block">
                <input type="text" name="url" placeholder="菜单地址" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <label class="layui-form-label">资源类型</label>
            <div class="layui-input-block">
                <input type="radio" name="type" value="0" title="模块">
                <input type="radio" name="type" value="1" title="菜单" checked="checked">
            </div>
        </div>
        <div class="layui-form-item layui-form-text layui-col-md11 layui-col-sm11">
            <label class="layui-form-label"><span style="color: red;">*</span>排序</label>
            <div class="layui-input-block">
                <input type="text" name="rank" lay-verify="number" placeholder="联系排序序号" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <div class="layui-input-block" style="text-align: center;">
                <button class="layui-btn" lay-submit="" lay-filter="formSubmit">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</fieldset>
<script src="../assets/layui/layui.js"></script>
<!-- jQuery -->
<script type="text/javascript" src="../assets/jquery-1.12.4.min.js"></script>
<!-- fontIconPicker JS -->
<script type="text/javascript" src="../assets/lib/fonticonpicker/js/jquery.fonticonpicker.min.js"></script>
<script>
    var fonts=['fa fa-table',
        'fa fa-address-book-o',
        'fa fa-address-card-o',
        'fa fa-anchor',
        'fa fa-area-chart',
        'fa fa-archive',
        'fa fa-bar-chart',
        'fa fa-university',
        'fa fa-bars',
        'fa fa-book',
        'fa fa-bookmark-o',
        'fa fa-briefcase',
        'fa fa-pie-chart',
        'fa fa-pencil-square-o',
        'fa fa-paper-plane',
        'fa fa-server',
        'fa fa-user-o',
        'fa fa-flag-o',
        'fa fa-folder-open-o',
        'fa fa-calendar',
        'fa fa-calendar-minus-o',
        'fa fa-cogs',
        'fa fa-comment-o',
        'fa fa-cubes',
        'fa fa-database',
        'fa fa-desktop',
        'fa fa-tachometer',
        'fa fa-envelope-o',
        'fa fa-pencil-square-o',
        'fa fa-heartbeat',
        'fa fa-line-chart',
        'fa fa-map',
        'fa fa-music',
        'fa fa-umbrella','fa fa-cloud','fa fa-futbol-o','fa fa-globe','fa fa-shield'];
    var iconElement;
    jQuery(document).ready(function($) {
        iconElement=$('#ipt_icon').fontIconPicker({
            source: fonts,
            hasSearch: false
        });
    });

    layui.config({
        base: '../js/'
    }).extend({
        iconPicker: 'iconPicker/iconPicker'
    });
    layui.use(['jquery','form','validate','common'], function () {
        var form = layui.form,
            $ = layui.jquery,
            layer = layui.layer,
            validate = layui.validate;
        common = layui.common;
        validate.regVerify();//注册验证
        var id = common.getUrlParam("id");
        var type = common.getUrlParam("type");

//        iconPicker.render({
//            // 选择器，推荐使用input
//            elem: '#ipt_icon',
//            // 数据类型：fontClass/unicode，推荐使用fontClass
//            type: 'fontClass',
//            // 是否开启搜索：true/false
//            search: true,
//            limit: 12,
//            // 点击回调
//            click: function (data) {
//                console.log(data);
//            }
//        });

        common.initSelectData("#ddl_parentId","res/resTree4Select",function () {
            if (type=="edit"){
                //加载数据
                $("#ipt_id").attr("readonly","readonly");

                //获取用户信息
                $.ajax({
                    url: "res/info",
                    dataType: "json",
                    data: { id: id },
                    success: function (rtn) {
                        if (rtn && rtn.code===0) {
                            var data = rtn.data;
                            form.val('dataForm',{
                                id:data.id,
                                name:data.name,
                                parentId:data.parentId,
                                rank:data.rank,
                                url:data.url,
                                type:data.type,
                                icon:data.icon
                            });
                            if(data.icon){
                                iconElement.setIcon(data.icon);
                            }
                        }
                    }
                });
            }
        });

        //监听提交
        form.on('submit(formSubmit)', function (data) {
            //common.consoleDebug(JSON.stringify(data.field));
            $.ajax({
                type: 'post',
                url: 'res/'+(type=="edit"?"edit":"add"),
                data: data.field,
                success: function(rtn){
                    if (rtn.code===0) {
                        common.showInfo('处理成功!',function(){
                            //刷新前页
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.document.getElementById('btnSearch').click();
                            parent.layer.close(index);
                        });
                    }
                    else{
                        common.showError(rtn.msg);
                    }
                },
                error: function(XmlHttpRequest, textStatus, errorThrown){
                    common.showError('保存错误');
                }
            });
            return false;
        });
    });
</script>
</body>
</html>